<?php

namespace app\system\controller\logic;

use think\Db;
use think\Request;

class SelectController extends \app\system\controller\BaseController
{


    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * 绑定司机
     * @return mixed
     */
    public function dirver(){
        $pageNumber = $this->request->param('pageNumber/d'); //当前页数
        $pageSize = $this->request->param('pageSize/d',100); //每页记录数
        $orderBy = $this->request->param('orderBy/a');
        $searchField = $this->request->param('searchField/s');
        $q_word = $this->request->param('q_word/a'); //多个关键词结合andOr检索
        $searchKey = $this->request->param('searchKey/s'); //查询初始化数据
        $searchValue = $this->request->param('searchValue/s'); //查询初始化数据
        try{
            //处理排序
            if(!empty($orderBy)){
                foreach($orderBy as $v){
                    $order[] = "{$v[0]} $v[1]";
                }
                $order = implode(',',$order);
            }
            //处理模糊多关键词检索搜索
            $map=[];
            if(!empty($q_word)){
                $query=[];
                foreach($q_word as $v){
                    $query[]='%'.$v.'%';
                }
                $map[$searchField] = ['like',$query,'OR'];
            }
            //处理默认选中
            if(!empty($searchKey) && !empty($searchValue)){
                $map[$searchKey] = ['in',$searchValue];
            }
            $uids = Db::table('car_driver')
                ->where('work_status',1)
                ->where('dri_status',2)
                ->distinct('bind_uid')->column('bind_uid');
            if(!empty($uids)){
                $map['uid'] = ['in',$uids];
            }
            $data['totalRow']=Db::table('user')->where($map)->count();
            $data['list']=Db::table('user')->where($map)->page($pageNumber,$pageSize)->order($order)->select();
            //$data['sql']=Db::table('car_driver')->getLastSql();
        } catch (\Exception $e){
            echo 'server error:'.$e->getMessage();
        }
        return json($data);
    }
	//司机
	public function selectsj(){
		 $pageNumber = $this->request->param('pageNumber/d'); //当前页数
        $pageSize = $this->request->param('pageSize/d',100); //每页记录数
        $orderBy = $this->request->param('orderBy/a');
        $searchField = $this->request->param('searchField/s');
        $q_word = $this->request->param('q_word/a'); //多个关键词结合andOr检索
        $searchKey = $this->request->param('searchKey/s'); //查询初始化数据
        $searchValue = $this->request->param('searchValue/s'); //查询初始化数据
        try{
            //处理排序
            if(!empty($orderBy)){
                foreach($orderBy as $v){
                    $order[] = "{$v[0]} $v[1]";
                }
                $order = implode(',',$order);
            }
            //处理模糊多关键词检索搜索
            $map=[];
            if(!empty($q_word)){
                $query=[];
                foreach($q_word as $v){
                    $query[]='%'.$v.'%';
                }
                $map[$searchField] = ['like',$query,'OR'];
            }
            //处理默认选中
            if(!empty($searchKey) && !empty($searchValue)){
                $map[$searchKey] = ['in',$searchValue];
            }
            $map['group_id'] = 104;
            $data['totalRow']=Db::table('user')->where($map)->count();
            $data['list']=Db::table('user')->where($map)->page($pageNumber,$pageSize)->order($order)->select();
            //$data['sql']=Db::table('car_driver')->getLastSql();
        } catch (\Exception $e){
            echo 'server error:'.$e->getMessage();
        }
        return json($data);
	}
    //选择车辆
    public function car(){
        $query = $this->request->param();
        $carModel = cms_moudle(5);
        /*
        $orderModel = cms_moudle(14);
        $btime = $this->request->param('btime');
        $etime = $this->request->param('etime');
        $dateDay = $this->request->param('date_day/d',0);
        $query['date_day']=$dateDay;
        $btime = strtotime($btime)-$dateDay*86400;
        $etime = strtotime($etime)+$dateDay*86400;
        $carSns = $orderModel->distinct('car_sn')
            ->where("(order_start_time<{$etime} and {$btime}< order_end_time ) or (order_start_time<{$btime} and order_end_time>{$etime})")
            //->where("(order_start_time>{$btime} and {$etime}< order_start_time ) or (order_end_time<{$btime} and order_end_time<{$etime})")
            ->column('order_start_time,order_end_time','car_sn');
        //dump($carSns);
        //echo $orderModel->getLastSql();
        if($carSns){
            $omap['car_sn'] = ['not in',array_keys($carSns)];
        }
        */
        $seat = $this->request->param('seat',0); //座位数
        if($seat>0){
            $cmap['car_seat']=['>=',$seat];
        }
        //$cmap['car_status']=0;
        $car_sn = $this->request->param('car_sn');
        if($car_sn){
            $cmap['car_sn'] = ['like','%'.$car_sn.'%'];
        }
        $list = $carModel->where($cmap)->order('btype_aid asc')->select();
        $this->assign('query',$query);
        $this->assign('list',$list);
        return $this->fetch();
    }

    //选择司机
    public function driver(){
        $query = $this->request->param();
        $carDriver = cms_moudle(16); //司机
        $orderModel = cms_moudle(14);
        $aid = $this->request->param('aid/d');
        $orderInfo = $orderModel->where('aid',$aid)->find();
        /*
        $dateDay = $this->request->param('date_day/d',0);
        $query['date_day']=$dateDay;
        $query['btime']=$orderInfo['order_start_time'];
        $query['etime']=$orderInfo['order_end_time'];
        $btime = strtotime($orderInfo['order_start_time'])-$dateDay*86400;
        $etime = strtotime($orderInfo['order_end_time'])+$dateDay*86400;
        //fleet_driver ,隔开id
        $fleet_drivers = $orderModel->distinct('fleet_driver')
            ->where("(order_start_time<{$etime} and {$btime}< order_end_time ) or (order_start_time<{$btime} and order_end_time>{$etime})")
            ->where("fleet_driver<>''")
            //->where("(order_start_time>{$btime} and {$etime}< order_start_time ) or (order_end_time<{$btime} and order_end_time<{$etime})")
            ->column('order_start_time,order_end_time','fleet_driver_ids');
        if($fleet_drivers){
            $fleet_drivers= explode(',',implode(',',array_keys($fleet_drivers)));
            $omap['aid'] = ['not in',$fleet_drivers];
        }
        */
        $title = $this->request->param('title/s');
        if(!empty($title)){
            $omap['title'] = ['like','%'.$title.'%'];
            $cmap['title'] = ['like','%'.$title.'%'];
        }
        //0|出车,1|请假,2|空闲
        //$cmap['dri_status']=2;
        $list = $carDriver->whereOr($omap)->where($cmap)->select();
        //echo $carDriver->getLastSql();
        //echo $carDriver->getLastSql();
        $this->assign('query',$query);
        $this->assign('list',$list);
        return $this->fetch();
    }

}
